﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using EntityObjects;

namespace PresentationLayer.View.StoreManager
{
    public partial class ApproveRejectItemAdjustment : System.Web.UI.Page
    {
        ItemAdjustmentControl control = new ItemAdjustmentControl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Adjustment> adjustList = control.getVouchers();

                gvAdjustmentList.DataKeyNames = new string[] { "adjVoucherId" };
                gvAdjustmentList.DataSource = adjustList;
                gvAdjustmentList.DataBind();

            }
            else
            {
                if (rdReject.Checked == true)
                {
                    remarkPanel.Visible = true;
                }
                else
                {
                    remarkPanel.Visible = false;
                }
            }
        }

        protected void gvAdjustmentList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            detailPanel.Visible = true;
            rdReject.Checked = false;
            rdApprove.Checked = false;
            remarkPanel.Visible = false;

            if (e.CommandName == "ViewDetail")
            {
                // Retrieve the row index stored in the 
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button 
                // from the Rows collection.
                GridViewRow row = gvAdjustmentList.Rows[index];

                HiddenField hdn = (HiddenField)row.FindControl("hdn_vid");
                string voucherId = hdn.Value;
                List<AdjustmentDetails> result = control.getAdjustmentDetailsByVoucherId(voucherId);
                gv_ViewDetail.DataSource = result;
                gv_ViewDetail.DataBind();

                hdnVId.Value = hdn.Value;

                Label lbl = (Label)row.FindControl("lblStatus");
                if (lbl.Text != "Pending")
                    statusPanel.Visible = false;
                else
                    statusPanel.Visible = true;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string status;
            string vid = hdnVId.Value;
            string remark = txtremark.Text.Trim();
            if (rdApprove.Checked == true)
                status = "Approved";
            else
                status = "Rejected";

            Boolean flag = control.UpdateVoucherStatud(vid,status,remark);
            if (flag == true)
            {
                detailPanel.Visible = false;
                Label3.Text = " Successfully saved ";
            }
        }
    }
}